import { useModel } from "umi";
import { useReactive } from "ahooks";

import { FindAllCategories } from "../../_graphql/organization/category";

const useCategory = () => {
    const { graphqlFetchPlatform } = useModel("global");

    const state = useReactive({
        dataLoading: false,
        categories: [],
    });

    const loadCategories = async () => {
        state.dataLoading = true;
        let res = await graphqlFetchPlatform({
            query: FindAllCategories,
            variables: {},
        });
        state.categories = res?.a?.nodes || [];
        state.dataLoading = false;
    };

    return { ...state, loadCategories };
};

export default useCategory;
